package com.eeepay.eeepay_v2.util;

import android.os.Handler;
import android.os.HandlerThread;

/**
 * 描述：封装一个轻量级的任务处理器
 * 作者：zhuangzeqin
 * 时间: 2017/6/24-16:51
 * 邮箱：zzq@eeepay.cn
 */
public class LightTaskManager {
    private Handler mHandler;

    private LightTaskManager() {
        HandlerThread workerThread = new HandlerThread("LightTaskThread");
        workerThread.start();
        mHandler = new Handler(workerThread.getLooper());
    }

    public static final LightTaskManager getInstance() {
        return SingletonHolder.INSTANCE;
    }

    private static class SingletonHolder {
        private static final LightTaskManager INSTANCE = new LightTaskManager();
    }

    /**
     * 提交优先级一般的任务
     *
     * @param run
     */
    public void post(Runnable run) {
        mHandler.post(run);
    }

    /**
     * 将优先级较高的任务加入到队列前端
     *
     * @param runnable
     */
    public void postAtFrontOfQueue(Runnable runnable) {
        mHandler.postAtFrontOfQueue(runnable);
    }

    /**
     * 延后提交优先级较低的任务
     *
     * @param runnable
     * @param delay
     */
    public void postDelayed(Runnable runnable, long delay) {
        mHandler.postDelayed(runnable, delay);
    }

    /**
     * 指定时间提交任务
     *
     * @param runnable
     * @param time
     */
    public void postAtTime(Runnable runnable, long time) {
        mHandler.postAtTime(runnable, time);
    }
}
